package com.jesse.eclgy.plug.wrapper;


import com.jesse.eclgy.plug.enums.FieldType;
import com.jesse.eclgy.plug.util.ReflectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * @Author: jesse
 * @Date: 2023/6/3 12:45
 */
public class QueryWrapper implements IWrapper {

    /*
     * 查询字段
     */
    private Set<String> columnList;
    /*
     * 查询表名
     */
    private String tableName;
    /*
     * 查询的条件
     */
    private ConditionWrapper condition;
    /**
     * 分页显示总数
     */
    private int pageCount = 0;
    /**
     * 当前分页
     */
    private int pageIndex = 0;

    public static QueryWrapper builder()
    {
        return new QueryWrapper();
    }

    public QueryWrapper()
    {
        this.columnList = new LinkedHashSet<>();
    }

    /**
     * 查询表名
     *
     * @param tableName
     * @return
     */
    public QueryWrapper tableName(String tableName)
    {
        this.tableName = tableName;
        return this;
    }

    /**
     * 查询的条件
     *
     * @param condition
     * @return
     */
    public QueryWrapper condition(ConditionWrapper condition)
    {
        this.condition = condition;
        return this;
    }

    /**
     * 查询的条件
     *
     * @param conditionMap
     * @return
     */
    public QueryWrapper conditionMap(Map<String, Object> conditionMap)
    {
        if (this.condition == null)
        {
            this.condition = ConditionWrapper.builder();
        }
        this.condition.condition(conditionMap);
        return this;
    }

    /**
     * 查询的条件
     *
     * @param field
     * @param value
     * @return
     */
    public QueryWrapper conditionEQ(String field, Object value)
    {
        this.condition = this.condition == null ? ConditionWrapper.builder() : this.condition.and();
        this.condition.eq(field, value);
        return this;
    }


    /**
     * 查询的字段列
     *
     * @param column
     * @return
     */
    public QueryWrapper column(String... column)
    {
        Arrays.stream(column).forEach(c -> this.columnList.add(c));
        return this;
    }

    /**
     * 查询的字段列
     *
     * @param column
     * @return
     */
    public QueryWrapper column(Set<String> column)
    {
        this.columnList.addAll(column);
        return this;
    }

    /**
     * 分页限制
     *
     * @param pageIndex
     * @param pageCount
     * @return
     */
    public QueryWrapper limit(int pageIndex, int pageCount)
    {
        this.pageIndex = pageIndex;
        this.pageCount = pageCount;
        return this;
    }

    /**
     * 分页限制
     *
     * @param pageCount
     * @return
     */
    public QueryWrapper limit(int pageCount)
    {
        this.pageCount = pageCount;
        return this;
    }

    /**
     * 泛型转
     *
     * @param obj 泛型对象
     * @return
     */
    @Override
    public QueryWrapper inject(Object obj)
    {
        final Map<String, Object> fieldMap = ReflectionUtils.getFieldMap(obj, FieldType.QUERY);

        this.column(fieldMap.keySet());
        this.tableName(ReflectionUtils.getTableName(obj.getClass(), false));
        this.conditionMap(ReflectionUtils.getFieldMap(obj, FieldType.CONDITON));

        return this;
    }

    /**
     * 获取SQL语句
     *
     * @return
     */
    @Override
    public String getSql()
    {
        StringBuilder sql = new StringBuilder("SELECT ");

        if (this.columnList.isEmpty())
        {
            sql.append("*");
        } else
        {
            sql.append(String.join(",", this.columnList));
        }

        sql.append(" FROM ").append(this.tableName);
        if (null != this.condition && StringUtils.isNotBlank(this.condition.getSql()))
        {
            sql.append(" WHERE ").append(this.condition.getSql());
        }

        if (this.pageCount > 0 && this.pageIndex > 0)
        {
            sql.append(" LIMIT ")
                    .append((this.pageIndex - 1) * this.pageCount)
                    .append(",")
                    .append(this.pageCount);
        } else if (this.pageCount > 0)
        {
            sql.append(" LIMIT ").append(this.pageCount);
        }

        return sql.toString();
    }

    /**
     * 获取参数
     *
     * @return
     */
    @Override
    public List<Object> getParams()
    {
        return this.condition != null ? condition.getParams() : null;

    }
}
